﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mimoza.Common
{
    public class ContainerRecord
    {
        public Guid ID;
        public Guid ParentID;
        public string Name;
        public int Type;
        public bool IsSystem;
        public bool IsVisible;
        public Guid EffectivePolicyID;
        public DateTime UpdateDate;
    }

    public enum DBHostStatus { notdefined, Online, Offline };

    public class HostRecord
    {
        public Guid ID;
        public Guid ParentID;
        public string Name;
        public string NewName;
        public string Description;
        public string MACAddress;
        public string IPAddress;
        public string AdaptersInfo;
        public string OS;
        public string Model;
        public string Version;
        public DBHostStatus Status;
        public Guid ModificationID;
        public DateTime UpdateDate;
        public DateTime LastConnectionDate;
    }

    public class PolicyRecord
    {
        public Guid ID;
        public Guid ParentID;
        public string Name;
        public string Description;
        public string PluginID;
        public bool IsEnabled;
        public bool IsHardware;
        public bool IsMultiple;
        public bool Override;
        public Guid UpdatesID;
        public Guid DataModificationID;
        public string Data;
        public DateTime UpdateDate;
    }

    public class UpdateRecord
    {
        public Guid ID;
        public string Name;
        public string Description;
        public string OS;
        public string Model;
        public string Version;
        public string UpdateFileName;
        [NonSerialized]
        public byte[] UpdateFileBody;
    }

    public enum DBEventType { ObjectInserted, ObjectUpdated, ObjectDeleted};
    public class DBObjectChangedEventArgs : EventArgs
    {
        public DBObjectChangedEventArgs(object _obj, DBEventType _type)
        {
            obj = _obj;
            type = _type;
        }
        private object obj;
        private DBEventType type;

        public object Object
        {
            get { return obj; }
        }

        public DBEventType EventType
        {
            get { return type; }
        }

    }

    public interface IDBObjectChangedNotfier
    {
        event EventHandler<DBObjectChangedEventArgs> OnObjectChanged;
    }

    public interface IDBContainerRepository : IDBObjectChangedNotfier
    {
        ContainerRecord GetContainer(Guid ID);
        IEnumerable<ContainerRecord> GetChildsContainer(Guid containerID);
        IEnumerable<HostRecord> GetHosts(Guid containerID);
        IEnumerable<PolicyRecord> GetPolicies(Guid containerID);        

        void Insert(ContainerRecord container);
        void Update(ContainerRecord container);
        void Delete(ContainerRecord container);
    }

    public interface IDBHostRepository : IDBObjectChangedNotfier
    {
        HostRecord GetHost(Guid ID);
        HostRecord GetHost(string MACAddress);
        HostRecord GetHostByName(string hostName);

        void Insert(HostRecord host);
        void Update(HostRecord host);
        void Delete(HostRecord host);
    }

    public interface IDBPolicyRepository : IDBObjectChangedNotfier
    {
        PolicyRecord GetPolicy(Guid ID);
        IEnumerable<PolicyRecord> GetPolicies(Guid containerID);

        void Insert(PolicyRecord policy);
        void Update(PolicyRecord policy);
        void Delete(PolicyRecord policy);
    }

    public interface IDBUpdateRepository : IDBObjectChangedNotfier
    {
        UpdateRecord GetUpdate(Guid ID);
        IEnumerable<UpdateRecord> GetUpdates();

        void Insert(UpdateRecord update);
        void Update(UpdateRecord update);
        void Delete(UpdateRecord update);
    }

    public interface IDBLayer
    {
        void Connect();
        void ConnectToHostDB();
        void Disconnect();

        IDBContainerRepository DBContainerRepository { get; }
        IDBHostRepository DBHostRepository { get; }
        IDBPolicyRepository DBPolicyRepository { get; }
        IDBUpdateRepository DBUpdateRepository { get; }
    }
}
